package com.portal.business.user.action;




import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import com.portal.business.user.UserService;
import com.portal.business.user.action.model.UserModel;
import com.portal.framework.exception.AppException;
import com.portal.framework.webutil.BaseAction;
import com.sys.business.custom.custominfo.dto.CustomInfoDTO;
import com.sys.system.version.dto.VersionDTO;


/**
 * 
 * 项目名称: Portal     
 * 类名称:   UserAction     
 * 类描述:   注册,下载,找回密码 
 * 创建人:   骆海东
 * 创建时间: Aug 31, 2011 10:15:58 AM     
 * 最后修改人:   骆海东
 * 最后修改时间: Aug 31, 2011 10:15:58 AM     
 * 最后修改备注:
 */
public class UserAction extends BaseAction {
	    
	    
	private static final long serialVersionUID = 4790076857995147525L;

	private UserModel model = new UserModel();
	
	private UserService userService;
	
	public String goIndex() {
		return SUCCESS;
	}
	
	/**
	 * 添加前
	 * 
	 * @return
	 * @throws AppException
	 */
	public String goUserAdd() {
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * 
	 * @return
	 * @throws AppException
	 */
	public String doUserAdd() {
		CustomInfoDTO customInfoDTO=this.model.getCustomInfoDTO();
		CustomInfoDTO c=this.userService.addCustomInfo(customInfoDTO);
		this.model.setCustomInfoDTO(c);
		return SUCCESS;
	}
	/**
	 * 
	 * 方法名:   validateCompanyName   
	 * 方法描述: 验证公司名称唯一性
	 * 传入参数:  
	 * 返回值类型: void
	 * 异常对象:
	 */
	public void validateCompanyName(){
		//取输入公司名称
		String name = getRequest().getParameter("companyName");
		name=this.changeURLDecoder(name);
		String statues="1";
		statues=this.userService.validateCompanyName(name);
		this.outputDateinfo(statues);
	}
	
	/**
	 * 
	 * 方法名:   validateCompanyMail   
	 * 方法描述:  验证公司邮箱唯一性
	 * 传入参数:  
	 * 返回值类型: void
	 * 异常对象:
	 */
	public void validateCompanyMail(){
		//取输入公司名称
		String mailAddress = getRequest().getParameter("mailAddress");
		mailAddress=this.changeURLDecoder(mailAddress);
		String statues="1";
		statues=this.userService.validateCompanyMail(mailAddress);
		this.outputDateinfo(statues);
	}

	/**
	 * 
	 * 方法名:   goSendPassword   
	 * 方法描述: 进入找回密码页面
	 * 传入参数:  
	 * 返回值类型: String
	 * 异常对象:
	 */
	public String goSendPassword(){
		return SUCCESS;
	}
	/**
	 * 
	 * 方法名:   doSendPassword   
	 * 方法描述: 执行找回密码
	 * 传入参数:  
	 * 返回值类型: String
	 * 异常对象:
	 */
	public String doSendPassword(){
		CustomInfoDTO customInfoDTO=this.model.getCustomInfoDTO();
		String mailAddress = customInfoDTO.getMailAddress();
		mailAddress=this.changeURLDecoder(mailAddress);
		String statues="0";
		statues=this.userService.sendPasswordMail(mailAddress);
		this.model.setEditStatues(statues);
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法名:   goDownLoad   
	 * 方法描述: TODO
	 * 传入参数:  
	 * 返回值类型: String
	 * 异常对象:
	 */
	public String goDownLoad(){
		this.model.setVersionDTO(this.userService.findNewVersion());
		return SUCCESS;
	}
	/**
	 * 
	 * 方法名:   doDownLoad   
	 * 方法描述: 执行软件下载
	 * 传入参数:  
	 * 返回值类型: String
	 * 异常对象:
	 * @throws IOException 
	 */
	public void doDownLoad() throws IOException{
		  //关于文件下载时采用文件流输出的方式处理：
		VersionDTO v=this.userService.findNewVersion();
//		 String   separator   =   System.getProperties().getProperty("file.separator");
		 String filedownload =v.getFileUrl();//下载文件物理地址
		 File f=new File(filedownload);
		 Integer len=Integer.parseInt(f.length()+"");//获取文件大小
		 if(f.exists()){
			 String urlName=v.getFileUrl();
			 String suffix=urlName.substring(urlName.lastIndexOf("."));
		 String filedisplay = v.getProductName()+v.getVersionNo()+suffix;
		 filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
		HttpServletResponse response= this.getResponse();
		 response.setContentType("application/x-download");
		 response.setHeader("Location",filedisplay);
		 response.setHeader("Cache-Control", "max-age=" + filedisplay);
		     //filename应该是编码后的(utf-8)
		 response.setHeader("Content-Disposition", "attachment; filename=" + filedisplay); 
		 response.setContentLength(len);//文件大小
		 OutputStream outputStream = response.getOutputStream();
		 InputStream inputStream = new FileInputStream(filedownload);
		 byte[] buffer = new byte[1024];
		 int i = -1;
		 while ((i = inputStream.read(buffer)) != -1) {
		  outputStream.write(buffer, 0, i);
		  }
		 outputStream.flush();
		 outputStream.close();
		 inputStream.close();
		 outputStream = null;
		 }else {
		 }
		
	}
	
	public UserModel getModel() {
		return model;
	}

	public void setModel(UserModel model) {
		this.model = model;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
